var express = require('express');
var router = express.Router();
var indexDao = require('../dao/indexDao');
var companyDao = require('../dao/companyDao');
var fitmentDao = require('../dao/fitmentDao');

router.get('/', function (req, res) {
    if(req.query.islogin && req.query.islogin == 1){
        if(req.query.username =='1' && req.query.password == '1'){
            res.render('index', {});
        }else{
            res.render('login', { title: '梦想改造家-登陆' });
        }
    }else{
        res.render('index', {});
    }


});

/* GET home page. */
router.get('/login', function(req, res, next) {
    res.render('login', { title: '梦想改造家-登陆' });
});

router.get('/register', function(req, res, next) {
    res.render('register');
});

router.get('/fitment_company', function (req, res) {
    res.render('src/views/fitment_company');
});

router.get('/fitment_advisor', function (req, res) {
    res.render('src/views/fitment_advisor');
});


router.get('/furniture_mall', function (req, res) {
    res.render('src/views/furniture_mall');
});

router.get('/user_center', function (req, res) {
    if(req.session.user){
        res.render('src/views/user_center');
    }else{
        res.render('login', { title: '梦想改造家-登陆' });
    }
});

/**
 * 获取装修步骤
 * 首页显示
 */
router.get('/fit_step', function (req, res) {
    indexDao.getFitStep(function (result) {
        res.json(result);
    })
});

/**
 * 获取装修案例
 * 首页显示
 */
router.get('/fit_case', function (req, res) {
    indexDao.getFitCase(function (result) {
        res.json(result);
    })
});
/**
 * 获取首页展示商品
 */
router.get('/fit_goods', function (req, res) {
    indexDao.getFitGoods(function (result) {
        res.json(result);
    })
});
/**
 * 获取首页攻略
 */
router.get('/fit_strategy', function (req, res) {
    indexDao.getFitStrategy(function (result) {
        res.json(result);
    })
});

/**
 * 装修公司页面 获取装修公司信息
 */
router.get('/fit_company', function (req, res) {
    companyDao.getFitCompany('', function (result) {
        res.json(result);
    })
});
/**
 * 筛选装修公司
 */
router.post('/filter_fit_company', function (req, res) {
    var filterData = req.body;
    companyDao.filterCompany(filterData)
        .then(function (result) {
            res.json(result);
        })

});

/**
 * 获取装修类型的路由
 */
router.get('/company_style_price', function (req, res) {
    companyDao.getCompanyStylePrice()
        .then(function (result) {
            res.json(result);
        });
});

/**
 * 商城页面，获取部分商品信息
 */
router.get('/mall_goods', function (req, res) {
    companyDao.getMallGoods('', function (result) {
        res.json(result);
    })
})
/**
 * 获取装修案例的详细信息，并跳转详细信息页面
 */
router.get('/case_detail', function (req, res) {
    res.render('src/views/fit_case_detail');
});
router.post('/getcasedetail', function (req, res) {
    var id = req.body.id;
     fitmentDao.getCaseDetail(id, function (result) {
         res.json({'detail': result});
     })
});
/**
 * 跳转商品信息页面
 */
router.get('/goods_detail', function (req, res) {
    res.render('src/views/fit_goods_detail');
});
router.post('/getgoodsdetail', function (req, res) {
    var id = req.body.id;
    fitmentDao.getGoodsDetail(id, function (result) {
        res.json(result);
    })
})
/**
 * 提娜佳购物车
 */
router.post('/addCart', function (req, res) {
    if(req.session.user){
        var data = req.body;
        data.tel = req.session.user;
        fitmentDao.addCart(data)
            .then(function (result) {
                res.json({'ok':1});
            });
    }else{
       res.json({'nologin':1});
    }

});
/**
 * 跳转立即购买页面
 */
router.get('/buynow', function (req, res) {
    if(req.session.user){
        res.json({'islogin':1})
    }else{
        res.json({'islogin':0})
    }

});
/**
 * 跳转立即购买页面
 */
router.get('/buy_now', function (req, res) {
    res.render('src/views/buy_now');
})
/**
 * 获取攻略
 */
router.get('/getStrategy', function (req, res) {
    fitmentDao.getStrategy(req.query)
        .then(function (result) {
            res.json(result);
        });
})
/**
 * 跳转攻略详情页面
 */
router.get('/advisor_detail', function (req, res) {
    res.render('src/views/advisor_detail');
})
/**
 * 获取攻略详情
 */
router.post('/getAdvisorDetail', function (req, res) {
    fitmentDao.getAdvisorDetail(req.body.id)
        .then(function (result) {
            res.json(result);
        });
})

module.exports = router;
